package com.yuandian.management.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuandian.api.customer_perception.po.BusinessAnalysisParams;
import com.yuandian.api.customer_perception.vo.BusinessStepAnalysisVo;
import com.yuandian.api.management.entity.ConfStep;
import com.yuandian.api.management.entity.ConfStepJoinMapping;
import com.yuandian.api.management.validate.InsertFlag;
import com.yuandian.api.management.vo.OptionVo;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.bpm.common.log.annotation.SysLog;
import com.yuandian.management.service.ConfStepJoinMappingService;
import com.yuandian.management.service.ConfStepService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

/**
 * 步骤配置表-Controller
 * @date 2023-3-14
 */
@Api(tags = "业务监控配置-业务步骤配置")
@RestController
@RequestMapping("/step")
public class ConfStepController {

	@Autowired
	private ConfStepService confStepService;

	@Autowired
	private ConfStepJoinMappingService confStepJoinMappingService;

	/**
	 * 通过主键查询数据
	 * @param idList 主键集合
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过主键查询数据", notes = "通过主键查询数据，支持批量查询")
	@GetMapping("/{ids}")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "步骤id(可多选)", paramType = "query", required = true, dataTypeClass = Integer.class)
	})
	public R<List<ConfStep>> queryByIds(@PathVariable("ids") List<Integer> idList) {
		return R.ok(confStepService.listByIds(idList));
	}

	/**
	 * 分页查询
	 * @param confStep 筛选条件
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@PostMapping
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "步骤id", paramType = "body", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "systemId", value = "所属系统id", paramType = "body", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "businessCode", value = "所属业务编码", paramType = "body", required = false, dataType = "String"),
			@ApiImplicitParam(name = "isConfirm", value = "是否确认，0：否(默认)、1：是", paramType = "body", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "name", value = "步骤名称", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "pageSize", value = "每页大小", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "pageNum", value = "当前页数", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "orderByColumn", value = "排序列", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "isAsc", value = "排序的方向值只能是 desc 或者 asc", paramType = "body", required = true, dataType = "String")
	})
	public R<Page<ConfStep>> listPage(@RequestBody @ApiIgnore ConfStep confStep) {
		return R.ok(confStepService.listPage(confStep));
	}

	/**
	 * 新增数据
	 * @param confStep 实例对象
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "新增数据", notes = "新增数据")
	@SysLog("新增步骤配置表数据")
	@PostMapping("/save")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId", 		value = "所属系统id",	 			    paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "businessCode", 	value = "所属业务编码", 	 			    paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "stepCode", 		value = "步骤编码(绑定的业务服务资源编码)",  paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "name", 			value = "步骤名称", 		 				paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "type", 			value = "步骤类型，1：首步骤、2：中间步骤、3：末步骤",  paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "isAllowFail", 	value = "是否允许失败，0：否(默认)、1：是",  paramType = "body", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "isAllowLose", 	value = "是否允许缺失，0：否(默认)、1：是",  paramType = "body", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "pageSize", 		value = "每页大小", 					    paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "pageNum", 		value = "当前页数", 						paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "orderByColumn", 	value = "排序列", 						paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "isAsc", 			value = "排序的方向值只能是 desc 或者 asc", paramType = "body", required = true, dataType = "String")
	})
	public R<String> save(@Validated @RequestBody @ApiIgnore ConfStep confStep) {
		return confStepService.saveStep(confStep);
	}

	/**
	 * 通过主键更新数据
	 * @param confStep 实例对象
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过主键更新数据", notes = "通过主键更新数据")
	@SysLog("更新步骤配置表数据")
	@PutMapping
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "步骤id", paramType = "body", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "systemId", value = "所属系统id", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "businessCode", value = "所属业务编码", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "stepCode", value = "步骤编码(绑定的业务服务资源编码)", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "name", value = "步骤名称", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "type", value = "是否允许失败，0：否(默认)、1：是", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "isAllowFail", value = "是否允许失败，0：否(默认)、1：是", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "isAllowLose", value = "是否允许缺失，0：否(默认)、1：是", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "pageSize", value = "每页大小", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "pageNum", value = "当前页数", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "orderByColumn", value = "排序列", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "isAsc", value = "排序的方向值只能是 desc 或者 asc", paramType = "body", required = true, dataType = "String")
	})
	public R<Boolean> updateById(@Validated(InsertFlag.class) @RequestBody @ApiIgnore ConfStep confStep) {
		return confStepService.updateStepById(confStep);
	}

	/**
	 * 通过主键删除数据
	 * @param ids 主键
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过主键删除数据", notes = "通过主键删除数据")
	@SysLog("删除步骤配置表数据")
	@DeleteMapping("/delete/{ids}")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "步骤id(支持多选)", paramType = "query", required = false, dataType = "Integer"),
	})
	public R<Boolean> removeById(@PathVariable("ids") List<Integer> ids){
		return R.ok(confStepService.removeByIds(ids));
	}

	/**
	 * 通过主键确认步骤
	 * @param ids 主键
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过主键确认步骤", notes = "通过主键确认步骤")
	@GetMapping("/confirm/{ids}")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "步骤id(支持多选)", paramType = "query", required = false, dataType = "Integer"),
	})
	public R<String> confirm(@PathVariable("ids") List<Integer> ids){
		return confStepService.confirm(ids, true);
	}

	/**
	 * 通过主键取消步骤
	 * @param ids 主键
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过主键确认步骤", notes = "通过主键确认步骤")
	@GetMapping("/cancel/{ids}")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "步骤id(支持多选)", paramType = "query", required = false, dataType = "Integer"),
	})
	public R<String> cancel(@PathVariable("ids") List<Integer> ids){
		return confStepService.confirm(ids, false);
	}

	/**
	 * 查询业务步骤类型
	 * @return 通用返回对象
	 */
	@ApiIgnore
	@ApiOperation(value = "查询业务步骤类型", notes = "查询业务步骤类型")
	@GetMapping("/queryStepTypeByBusinessCode")
	public R<Map<String,String>> queryStepTypeByBusinessCode(){
		return R.ok(confStepService.queryStepTypeByBusinessCode());
	}

	/**
	 * 查询步骤编码和名称
	 * @return 通用返回对象
	 */
	@ApiIgnore
	@PostMapping("/getStepBySystemIdOrStepCode")
	@ApiOperation(value = "查询步骤编码和名称", notes = "查询步骤编码和名称")
	public R<List<ConfStep>> getStepBySystemIdAndStepCode(@RequestBody ConfStep confStep){
		return R.ok(confStepService.getStepBySystemIdAndStepCode(confStep));
	}

	/**
	 * 查询步骤编码和名称
	 * @return 通用返回对象
	 */
	@ApiIgnore
	@PostMapping("/getStepByBusinessCodes")
	@ApiOperation(value = "查询步骤编码和名称", notes = "查询步骤编码和名称")
	public R<List<ConfStep>> getStepByBusinessCodes(@RequestBody ConfStep confStep){
		return R.ok(confStepService.getStepByBusinessCodes(confStep));
	}


	/**
	 * 查询步骤对象
	 * @return 通用返回对象
	 */
	@ApiIgnore
	@PostMapping("/list")
	@ApiOperation(value = "查询步骤对象", notes = "查询步骤对象")
	public R<List<ConfStep>> list(@RequestBody ConfStep confStep){
		return R.ok(confStepService.list(Wrappers.query(confStep)));
	}


	/**
	 * 查询共用步骤的步骤编码和步骤名称
	 * @return 通用返回对象
	 */
	@ApiIgnore
	@GetMapping("/queryPublicStepOptions")
	@ApiOperation(value = "查询共用步骤的步骤编码和步骤名称", notes = "查询共用步骤的步骤编码和步骤名称")
	public R<List<OptionVo>> queryPublicStepOptions() {
		return R.ok(confStepService.queryPublicStepOptions());
	}


	/**
	 * 查询系统id
	 * @return 通用返回对象
	 */
	@ApiIgnore
	@PostMapping("/querySystemIdByStepCode")
	@ApiOperation(value = "查询系统id", notes = "查询系统id")
	public R<List<String>> querySystemIdByStepCode(@RequestBody List<String> stepCodes) {
		return R.ok(confStepService.querySystemIdByStepCode(stepCodes));
	}

	/**
	 * 详单配置组合策略
	 * @param confStepJoinMapping 关联配置
	 * @return 通用返回对象
	 */
	@PostMapping("/getStrategy")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "stepId", value = "步骤id", paramType = "body", required = true, dataType = "Integer"),
	})
	@ApiOperation(value = "详单配置组合策略", notes = "详单配置组合策略")
	public R<ConfStepJoinMapping> getStrategy(@RequestBody @ApiIgnore ConfStepJoinMapping confStepJoinMapping) {
		return R.ok(confStepJoinMappingService.getStrategy(confStepJoinMapping));
	}

	/**
	 * 保存详单配置组合策略
	 * @param confStepJoinMapping 关联配置
	 * @return 通用返回对象
	 */
	@PostMapping("/saveStrategy")
	@ApiOperation(value = "保存详单配置组合策略", notes = "保存详单配置组合策略")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "步骤id", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "businessCode", value = "业务编码", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "systemId", value = "系统id", paramType = "body", required = true, dataType = "String"),
	})
	public R saveStrategy(@RequestBody @ApiIgnore ConfStepJoinMapping confStepJoinMapping) {
		return confStepJoinMappingService.saveStrategy(confStepJoinMapping);
	}

	/**
	 * 查询同一系统共用步骤的业务
	 * @param stepCode 步骤编码
	 * @param stepId
	 * @return 通用返回对象
	 */
	@PostMapping("/getShareBusiness/{systemId}/{stepId}/{stepCode}")
	@ApiOperation(value = "查询同一系统共用步骤的业务", notes = "查询同一系统共用步骤的业务")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId", value = "系统id", paramType = "path", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "stepId", value = "步骤id", paramType = "path", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "stepCode", value = "步骤编码", paramType = "path", required = true, dataType = "String"),
	})
	public R getShareBusiness(@PathVariable("stepCode") @ApiIgnore String stepCode, @PathVariable("systemId") Integer systemId, @PathVariable("stepId") Integer stepId) {
		return confStepService.getShareBusiness(stepCode, systemId, stepId);
	}

	/**
	 * 根据业务编码查询对应业务步骤信息
	 * @param params
	 * @return
	 */
	@PostMapping("/selectStepsByBusicessCode")
	public R<List<BusinessStepAnalysisVo>> selectStepsByBusicessCode(@RequestBody BusinessAnalysisParams params){
		return R.ok(this.confStepService.selectStepsByBusicessCode(params));
	}

}
